/****************说明*****************/
// 1.事件中心
// 2.字典实现
// 3. 无参 泛型参数 泛型双参数
// 4.使用委托实现
using System.Collections.Generic;
using UnityEngine.Events;

namespace FrameWork
{
    public interface IEvent
    {
    }

    public class EventInfo : IEvent
    {
        public UnityAction action;

        public EventInfo(UnityAction action)
        {
            this.action = action;
        }
    }

    public class EventInfo<T> : IEvent
    {
        public UnityAction<T> action;

        public EventInfo(UnityAction<T> action)
        {
            this.action = action;
        }
    }
    
    public class EventInfo<T, U> : IEvent
    {
        public UnityAction<T, U> action;

        public EventInfo(UnityAction<T, U> action)
        {
            this.action = action;
        }
    }

    public class EventSystem : Singleton<EventSystem>
    {
        private Dictionary<string, IEvent> eventDic = new Dictionary<string, IEvent>();

        #region 泛型双参数

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="name"></param>
        /// <param name="action"></param>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        public void AddListener<T, U>(string name, UnityAction<T, U> action)
        {
            if (eventDic.ContainsKey(name))
            {
                (eventDic[name] as EventInfo<T, U>).action += action;
            }
            else
            {
                eventDic.Add(name, new EventInfo<T, U>(action));
            }
        }

        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="name"></param>
        /// <param name="action"></param>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        public void RemoveListener<T, U>(string name, UnityAction<T, U> action)
        {
            if (eventDic.ContainsKey(name))
            {
                (eventDic[name] as EventInfo<T, U>).action -= action;
            }
        }

        /// <summary>
        /// 广播触发
        /// </summary>
        /// <param name="name"></param>
        /// <param name="info1"></param>
        /// <param name="info2"></param>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        public void EventTrigger<T, U>(string name, T info1, U info2)
        {
            if (eventDic.ContainsKey(name))
            {
                (eventDic[name] as EventInfo<T, U>).action?.Invoke(info1, info2);
            }
        }

        #endregion

        #region 泛型单参数

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="name"></param>
        /// <param name="action"></param>
        /// <typeparam name="T"></typeparam>
        public void AddListener<T>(string name, UnityAction<T> action)
        {
            if (eventDic.ContainsKey(name))
            {
                (eventDic[name] as EventInfo<T>).action += action;
            }
            else
            {
                eventDic.Add(name, new EventInfo<T>(action));
            }
        }

        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="name"></param>
        /// <param name="action"></param>
        /// <typeparam name="T"></typeparam>
        public void RemoveListener<T>(string name, UnityAction<T> action)
        {
            if (eventDic.ContainsKey(name))
            {
                (eventDic[name] as EventInfo<T>).action -= action;
            }
        }

        /// <summary>
        /// 广播触发
        /// </summary>
        /// <param name="name"></param>
        /// <param name="info"></param>
        /// <typeparam name="T"></typeparam>
        public void EventTrigger<T>(string name, T info)
        {
            if (eventDic.ContainsKey(name))
            {
                (eventDic[name] as EventInfo<T>).action?.Invoke(info);
            }
        }

        #endregion

        #region 无参

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="name"></param>
        /// <param name="action"></param>
        public void AddListener(string name, UnityAction action)
        {
            if (eventDic.ContainsKey(name))
            {
                (eventDic[name] as EventInfo).action += action;
            }
            else
            {
                eventDic.Add(name, new EventInfo(action));
            }
        }

        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="name"></param>
        /// <param name="action"></param>
        public void RemoveListener(string name, UnityAction action)
        {
            if (eventDic.ContainsKey(name))
            {
                (eventDic[name] as EventInfo).action -= action;
            }
        }

        /// <summary>
        /// 广播触发
        /// </summary>
        /// <param name="name"></param>
        public void EventTrigger(string name)
        {
            if (eventDic.ContainsKey(name))
            {
                (eventDic[name] as EventInfo).action?.Invoke();
            }
        }

        #endregion

        public void Clear()
        {
            eventDic.Clear();
        }
    }
}